package com.migu.rbac.remote;

import com.migu.rbac.util.Response;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 使用RestTemplate调用外部接口
 * create by sz
 */
@Service
public class ValidateTokenRestTemplate {

    @Autowired
    RestTemplate restTemplate;

    /**
     * 调用portal-sso中的验证token的方法
     * @param url
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback")
    public Response validateToken(String url) {
        return restTemplate.getForObject(url, Response.class);
    }

    /**
     * 发生熔断时调用的Fallback方法
     * @param url
     * @param throwable
     * @return
     */
    public Response fallback(String url, Throwable throwable) {
        Response response = new Response();
        response.setIsSuccess(true);
        response.setErrorMsg("----服务调用发生熔断，所有请求接口放行----");
        return response;
    }

}
